package com.easybuy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easybuy.pojo.News;
import com.easybuy.pojo.Product;
import com.easybuy.service.NewsService;
import com.easybuy.util.PageBean;

@Controller
@RequestMapping(value = "/news")
public class NewsController {

	@Resource
	private NewsService newsService;
	
	@RequestMapping("/news")
	@ResponseBody
	public List<News> selectList() {
		return newsService.selectNews();
	}
	
	@RequestMapping(value = "/getList")
	@ResponseBody
	public Object selectNewsList(@RequestParam(value = "pageIndex",required = false) String pageIndex){
		PageBean<News> pagebean = new PageBean<News>();
		int count = newsService.selectNewsCount();
		pagebean.setCount(count);
		int pageNo= -1;
		if(pageIndex !=null && pageIndex.length()>0) {
			pageNo = Integer.parseInt(pageIndex);
		}else {
			pageNo = 1;
		}
		pagebean.setPageNow(pageNo);
		pagebean.setPageSize(10);
		int pagecount = count%10==0?count/10:count/10+1;
		pagebean.setPageCount(pagecount);
		Map<String, Object> params = new HashMap<String,Object>();
		if(!(params.containsKey("pageNow") && params.containsKey("pageSize"))) {
			params.put("pageNow", (pagebean.getPageNow()-1)*pagebean.getPageSize());
			params.put("pageSize", pagebean.getPageSize());
		}
		List<News> news = newsService.selectNewsList(params);
		pagebean.setList(news);
		return pagebean;
	}
	
	@RequestMapping(value = "/viewNews")
	@ResponseBody
	public Object selectNewsCount(@RequestParam(value = "id",required = false)String id) {
		Integer vid = Integer.parseInt(id);
		return newsService.selectContent(vid);
	}
	
}
